<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <title></title>
        
    </head>
    <body>
        <form action="" method="post">
            <?php
            //Author: Aya El Mobayad
            //The page functions.php is included so that the functions created inside of it can be 
            // accessible, and used within the page
            include 'functions.php';

            // A condition is added, checking whether the conference ID was passed from the previous page
            //in a get request using the isset function.
            if (isset($_GET['conference_id'])) {

                //The conference ID is retrieved from the previous page using a get request
                // and assigned to a variable
                $confID = intval($_GET['conference_id']);
            }
            //specifies the connection to the server, as well as the username and password and opens it.
            $con = mysql_connect('localhost', 'root', '');

            //The condition checks if there was an error in opening the connection
            if (!$con) {

                //In case the connection couldnt be opened, an error messsge is returned
                ('Could not connect:' . mysql_error());
            }
            //The database being used is selected from the open connection
            mysql_select_db("mydb", $con);

            //The conference long name is retrieved by calling the get_conference_name function from
            //functions.php and passing it the conference id of the conference for which the name
            //is needed, as a parameter.
            $confName = get_conference_name($confID);

            //The name retrieved using the previous function is printed at the top of the page
            //in a heading element
            print "<h1>{$confName}</h1>";

            //a heading is printed at the top of the page underneath the conference title.
            echo '<h2>call for papers</h2>';

            //a textarea is created for the call for papers text, and validates using the isAlphabet javascript function. 
            echo 'Announcement: </br><textarea id="announcement" name ="announcement" cols="50" rows="7" onblur="isAlphabetNum(this);"></textarea></br>';

            //a radiobutton list is created where the user chooses his sending options. He can either
            //send to all system users, or specific users.
            echo '</br><input type="radio" name="notify[]" value="all">send email notification to all system users.</input><br>
                <input type="radio" name="notify[]" value="sp">send email notification to specific users.</input><br><br>';

            //a server side button is created in order to execute the server side 
            //script for call for papers posting and sending. A link is added to redirect to a page where 
            //I can only choose people from a mailing list
            echo '<input type="submit" name="cfp" value="Send"/>';


            //The condition checks that the submit button was clicked
            if (isset($_POST['cfp'])) {
                //a variable is declared storing the value of the textarea contating the 
                //call for papers text
                $ann = $_POST['announcement'];

                //if there was no call for papers text in the textarea, a javascript alert appears
                //prompting the user to type something.
                if (!$ann) {
                    echo '<script>'
                    , 'alert("Please enter a text for the call for papers");'
                    , '</script>';
                    //In the case where there was text in the textarea
                } else {
                    //A query is executed in order to update the conference record with the same
                        //ID as the passed value with the text for the call for papers
                    mysql_query("UPDATE conference SET call_for_papers_text = '$ann'
                        WHERE conference_id = '$confID'") or die(mysql_error());

                    //the condition checks if a sending option is chosen from the radiobutton list
                    if (isset($_POST['notify'])) {
                        //the value of the radiobutton that was clicked is stored in a variable
                        $who = $_POST['notify'];
                        //if the "allusers" radio was selected
                        if ($who[0] == "all") {
                            //A query is executed to select the email adresses of all system users
                            //and the resultset is stored in a variable.
                            $allMembers = mysql_query("SELECT email FROM member");
                            //each row in the resultset is stored in an associative array on each loop iteration 
                            while ($memberRow = mysql_fetch_array($allMembers, MYSQL_ASSOC)) {
                                //the email address of each user is retrieved from the associative 
                                //array and placed in a variable email. This variable represents the recipient
                                $email = $memberRow['email'];
                                //a variable "subject" is initialized that represents the subject of the email.
                                $subject = ' - Call for Papers';
                                //a variable message is initialized containing the content of the email message.
                                $message = <<<EMAIL
                        This is an international call for papers for the "'$confName'" Conference:\n
                        '$ann' \n
                        For more Information please click this link:
                        http://ayaelmobayad.zapto.org:1234/ConfoDep/main.php?link=1&conference_id=$confID}
EMAIL;
                                //variable "from" specifies the sender
                                $from = 'info.confo@gmail.com';
                                //headers specifies the sender of the message
                                $headers = "From:" . $from;
                                //a mail function is called to send the message
                                mail($email, $subject, $message, $headers);
                            }
                            //an alert is shown when the call for papers is sent successfully
                            echo '<script type="text/javascript">'
                            , 'alert("call for papers sent successfully");'
                            , '</script>';
                        } 
                        ////if the specific users radio was selected
                        else if ($who[0] == "sp") {
                            //a header is created, redirecting to the page containing the system mailing list.
                            header('Location: MailingListDiv.php?conference_id=' . $confID . '&callforpapers=' . $ann . '');
                        }
                        
                    } else {
                        //If no sending options were chosen, an error message is alerted
                        echo '<script type="text/javascript">'
                        , 'alert("Please set sending options");'
                        , '</script>';
                    }
                }
            }

            //closes connection to database
            mysql_close($con);

            //End Documentation
            ?>


        </form>
    </body>
</html>
<head>
    <script src="sysJavascript.js" language="javascript">
    </script>
</head>
            
